package cdu.javaendwork.Controller.Admin.Goods;

import cdu.javaendwork.model.sqlModel.Goods;
import cdu.javaendwork.service.Impl.Shopping.GoodsServiceImpl;
import cdu.javaendwork.service.Interface.Shopping.GoodsService;
import cdu.javaendwork.util.pageHelp.PageHelper;
import cdu.javaendwork.util.pageHelp.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/goods_select")
public class GoodsSelectController extends HttpServlet {
    PageHelper pageHelper = new PageHelper();
    GoodsService goodsService=new GoodsServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset = utf-8");
        req.setCharacterEncoding("UTF-8");

        String goodsname = req.getParameter("goodsname");
        String sPage=req.getParameter("page");
        int pageSize=3;
        int pageIndex=(sPage == null || sPage.equals("")) ? 1 : Integer.parseInt(sPage);

        if(goodsname == ""){
            req.getRequestDispatcher("goods_list").forward(req,resp);
            return ;
        }
        List<Goods> goodsList=goodsService.filterByNameLimit(goodsname,pageIndex,pageSize);
        PageInfo pageInfo=pageHelper.filter("Goods", "name like '%" + goodsname + "%'",pageIndex,pageSize);

        req.setAttribute("goodsList",goodsList);
        req.setAttribute("pageInfo",pageInfo);

        req.setAttribute("goodsname" , goodsname);
        req.setAttribute("falg" , new Integer(1));


        req.getRequestDispatcher("goods_manage.jsp").forward(req,resp);

    }
}
